﻿namespace Blaze.Framework.Versions
{
    using System.IO;
    using System.Text;

    /// <summary>
    /// 对象文件信息。
    /// </summary>
    public class ObjectInfo
    {
        /// <summary>
        /// 对象的唯一哈希值。
        /// </summary>
        public byte[] Hash;

        /// <summary>
        /// 相对于根目录的路径。
        /// </summary>
        public string Path;

        /// <summary>
        /// 对象文件大小。
        /// </summary>
        public long Size;

        /// <summary>
        /// 计算哈希前缀。
        /// <example>4e14d0f1f609fec32a5b7c2f6f5b1da5 -> 4e/14d0f1f609fec32a5b7c2f6f5b1da5</example>
        /// </summary>
        /// <returns></returns>
        public string GetHashPath()
        {
            var buffer = new StringBuilder(33);
            buffer.AppendFormat("{0:x2}", Hash[0]);
            buffer.Append("/");
            for (var i = 1; i < Hash.Length; i++)
                buffer.AppendFormat("{0:x2}", Hash[i]);
            return buffer.ToString();
        }

        public void Load(BinaryReader reader)
        {
            Hash = reader.ReadBytes(16);
            Path = reader.ReadString();
            Size = reader.ReadInt64();
        }

        public void Write(BinaryWriter writer)
        {
            writer.Write(Hash);
            writer.Write(Path);
            writer.Write(Size);
        }
    }
}